package controller.behavior;

import java.awt.Frame;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import controller.JFrameController;
import model.BulletModel;
import model.TankModel;
import util.Direction;
import util.MyPosition;
import util.MyRectangle;
import view.BulletView;
import view.TankView;

public class ZeroHuman extends KeyAdapter {
    private boolean bL = false, bU = false, bR = false, bD = false;
    private boolean fire = false;
    private boolean kill = false;
    private TankModel model;
    private TankView view;
    private JFrameController controller;

    public ZeroHuman(TankModel model, TankView tankView, JFrameController controller) {
        this.model = model;
        this.view = tankView;
        this.controller = controller;

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        switch (key) {
            case KeyEvent.VK_LEFT:
                bL = true;
                break;
            case KeyEvent.VK_UP:
                bU = true;
                break;
            case KeyEvent.VK_RIGHT:
                bR = true;
                break;
            case KeyEvent.VK_DOWN:
                bD = true;
                break;
        }
        // FIXME 有一个bug，如果同时按下两个按键，则会对2个按键一起监听
        move();
    }

    @Override
    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();

        switch (key) {
            case KeyEvent.VK_X:
                fire = true;
                break;
            case KeyEvent.VK_LEFT:
                bL = false;
                break;
            case KeyEvent.VK_UP:
                bU = false;
                break;
            case KeyEvent.VK_RIGHT:
                bR = false;
                break;
            case KeyEvent.VK_DOWN:
                bD = false;
                break;
            case KeyEvent.VK_F2:
                kill = true;
                break;
        }
        fire();
    }

    private Direction getNewDir() {
        Direction direction = view.getDir();
        if (bL && !bU && !bR && !bD)
            direction = Direction.L;
        else if (bL && bU && !bR && !bD)
            direction = Direction.LU;
        else if (!bL && bU && !bR && !bD)
            direction = Direction.U;
        else if (!bL && bU && bR && !bD)
            direction = Direction.RU;
        else if (!bL && !bU && bR && !bD)
            direction = Direction.R;
        else if (!bL && !bU && bR && bD)
            direction = Direction.RD;
        else if (!bL && !bU && !bR && bD)
            direction = Direction.D;
        else if (bL && !bU && !bR && bD)
            direction = Direction.LD;
        else if (!bL && !bU && !bR && !bD)
            direction = Direction.STOP;

        return direction;
    }

    private void move() {
        if (kill == false) {
            view.setDir(getNewDir());

            MyPosition myPosition = controller.getNewPosition(model, view);
            boolean ismove = true;

            for (int i = 0; i < controller.getAis().size(); i++) {
                Behavior ai = controller.getAis().get(i);
                if (new MyRectangle(ai.getView().getPosition(), view.getPosition()).check()) {
                    if (ai instanceof EasyAI) {
                        ai.getView().stay();
                        ismove = false;
                        controller.beCollision(ai);
                    } else if (ai instanceof BulletAI) {
                        if (ai.getModel().getCamp() != model.getCamp()) {
                            int power = ((BulletModel) ai.getModel()).getPower();
                            double blood = power / (double) model.getBlood();
                            view.changeBlood(blood);
                            if (isLife() == false)
                                killSelf();
                            controller.beCollision(ai);
                        }
                    }
                }
            }

            if (ismove)
                view.setPosition(myPosition);
        }
    }

    private void fire() {
        if (kill == false) {
            if (fire) {
                try {
                    String bulletId = model.getBullet();
                    BulletModel bulletModel = new BulletModel(model.getCamp(), Integer.valueOf(bulletId));
                    BulletView bulletView = new BulletView(view.getPosition(), bulletModel, view.getFace());
                    Behavior bulletAI = new BulletAI(bulletModel, bulletView);
                    controller.getFrame().add(bulletView);
                    controller.getAis().add(bulletAI);
                } catch (NullPointerException e) {
                    System.out.println("子弹空了");
                }
                fire = !fire;
            }
        }
    }

    public MyPosition getPosition() {
        return view.getPosition();
    }

    private void killSelf() {
        model = null;
        view = null;
        kill = true;
        controller.getFrame().remove(view);
    }

    private void born(TankModel model, TankView tankView) {
        this.model = model;
        this.view = tankView;
        kill = true;
    }

    private boolean isLife() {
        if (view.getBloodView().getMode() <= 0)
            return false;
        else
            return true;
    }
}
